package com.zbkj.crmeb.category.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.category.dao.CategoryMenuTypeDao;
import com.zbkj.crmeb.category.model.CategoryMenuType;
import com.zbkj.crmeb.category.request.CategoryRequest;
import com.zbkj.crmeb.category.service.CategoryMenuTypeService;

/**
 * <p>
 * CategoryMenuTypeServiceImpl
 * </p>
 *
 * @author lvmd
 * @since 2022-02-16
 */
@Service
public class CategoryMenuTypeServiceImpl extends ServiceImpl<CategoryMenuTypeDao, CategoryMenuType> implements CategoryMenuTypeService {

    @Resource
    private CategoryServiceImpl categoryService;

    /**
     * 保存菜单身份类型
     *
     * @param categoryIds
     * @param identityTypes
     * @return
     */
    @Override
    public boolean saveCategoryMenuType(List<Integer> categoryIds, List<Integer> identityTypes) {
        if (CollectionUtils.isEmpty(identityTypes)) {
            throw new CrmebException("菜单身份类型不能为空！");
        }
        List<CategoryMenuType> list = new ArrayList<>(8);
        for (Integer categoryId : categoryIds) {
            identityTypes.forEach(type -> {
                CategoryMenuType categoryMenuType = new CategoryMenuType();
                categoryMenuType.setCategoryId(categoryId);
                categoryMenuType.setIdentityType(type);

                list.add(categoryMenuType);
            });
        }
        return this.saveBatch(list);
    }

    @Override
    public void addCategoryMenuType(CategoryRequest categoryRequest) {
        List<CategoryMenuType> list = new ArrayList<>(8);
        List<Integer> identityTypes = categoryRequest.getIdentityTypes();
        if (categoryRequest.getPid() > 0) {
            identityTypes = this.list(Wrappers.<CategoryMenuType>lambdaQuery()
                    .eq(CategoryMenuType::getCategoryId, categoryRequest.getPid()))
                    .stream().map(CategoryMenuType::getIdentityType).distinct().collect(Collectors.toList());
        }
        identityTypes.forEach(type -> {
            CategoryMenuType categoryMenuType = new CategoryMenuType();
            categoryMenuType.setCategoryId(categoryRequest.getId());
            categoryMenuType.setIdentityType(type);

            list.add(categoryMenuType);
        });
        this.saveBatch(list);
    }

    /**
     * 删除菜单身份类型
     * @param categoryIds  菜单分类id
     * @return
     */
    @Override
    public boolean deleteByCategoryId(List<Integer> categoryIds) {
        LambdaQueryWrapper<CategoryMenuType> lmq = new LambdaQueryWrapper<>();
        lmq.in(CategoryMenuType::getCategoryId, categoryIds);
        return baseMapper.delete(lmq) > 0;
    }

    /**
     * 修改菜单身份类型
     * @param id
     * @param identityTypes
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategoryMenuType(Integer id, List<Integer> identityTypes) {
        if (CollectionUtils.isEmpty(identityTypes)) {
            throw new CrmebException("菜单身份类型不能为空！");
        }
        List<Integer> cateIds = categoryService.getAllCateIds(id);
        if (CollectionUtils.isNotEmpty(cateIds)) {
            deleteByCategoryId(cateIds);
            return saveCategoryMenuType(cateIds, identityTypes);
        }
        return false;
    }

    /**
     * 查询菜单身份类型
     *
     * @param categoryIds
     * @return
     */
    @Override
    public Map<Integer, Set<Integer>> getMenuIdentityTypeMap(Integer identityType, List<Integer> categoryIds) {
        List<CategoryMenuType> list = baseMapper.selectList(Wrappers.<CategoryMenuType>lambdaQuery()
                .in(CategoryMenuType::getCategoryId, categoryIds)
                .eq(Objects.nonNull(identityType), CategoryMenuType::getIdentityType, identityType)
        );
        return list.stream().collect(Collectors.groupingBy(CategoryMenuType::getCategoryId,
                Collectors.mapping(CategoryMenuType::getIdentityType, Collectors.toSet())));
    }
}

